#include "fepch.h"
#include "Renderer.h"

namespace Fysics {
	Renderer::RenderData* Renderer::m_RenderData = new RenderData;

	void Renderer::BeginScene(const ViewCamera& camera)
	{
		m_RenderData->ViewMatrix = camera.GetViewMatrix();
		m_RenderData->ProjectionMatrix = camera.GetProjectionMatrix();
		m_RenderData->ModelMatrix = glm::mat4(1.0f);
		m_RenderData->ViewPos = camera.GetPosition();
	}

	void Renderer::EndScene()
	{
	}

	void Renderer::Init()
	{
		m_RenderData->ViewMatrix = glm::mat4(1.0f);
		m_RenderData->ProjectionMatrix = glm::mat4(1.0f);
		m_RenderData->ModelMatrix = glm::mat4(1.0f);
		m_RenderData->ViewPos = glm::vec3(0);
		m_RenderData->LightColor = glm::vec3(1.0f);
		m_RenderData->LightPos = glm::vec3(0);
	}

	void Renderer::PushObject(const Object* obj)
	{
		m_RenderData->ModelMatrix = obj->GetModelMatrix();
	}

	void Renderer::UpdateLight(const Light* light)
	{
		m_RenderData->LightColor = glm::vec3(1.0f, 1.0f, 1.0f);
		m_RenderData->LightPos = light->GetPos();
	}

	void Renderer::Submit(const std::shared_ptr<Shader>& shader, 
		const std::shared_ptr<VertexArray>& vertexArray)
	{
		shader->Bind();
		shader->UploadUniformMat4("u_Transform", 
			m_RenderData->ProjectionMatrix * m_RenderData->ViewMatrix * m_RenderData->ModelMatrix);
		shader->UploadUniformMat4("u_model", m_RenderData->ModelMatrix);
		shader->UploadUniformVec3("LightPos",m_RenderData->LightPos);
		shader->UploadUniformVec3("LightColor", m_RenderData->LightColor);
		shader->UploadUniformVec3("viewPos", m_RenderData->ViewPos);
		shader->UploadUniformVec3("objColor", glm::vec3(1.0f, 0.5f, 0.31f));
		vertexArray->Bind();
		RenderCommand::DrawIndexed(vertexArray);
	}

}